|
|
|
|
|
|
|
Print MonthName(12) 'Returns December
Print MonthName(12,True) 'Returns Dec |
|
|
|
|
|
|
|
|
The WeekDayName function also has an optional parameter that allows you to specify the starting day of the week: |
|
|
|
|
|
|
|
|
Print WeekDayName(1) 'Returns Sunday only if it is set to be
'the first day in Control Panel
Print WeekDayName(1,False,vbSunday) 'Always Returns Sunday |
|
|
|
|
|
|
|
|
One point to keep in mind about these functions is that they accept only numeric values. To find out the weekday or month for a specific date, you first have to use other Visual Basic functions to determine the numeric values. For example, suppose you want to find out the day of the week for February 18, 1991. You can use the WeekDay function to return the day number and then pass it to the WeekDayName function, as follows: |
|
|
|
|
|
|
|
|
Print WeekDayName(WeekDay("2/18/91")) |
|
|
|
|
|
|
|
|
A new function, the Round function, allows you to round a numeric expression to a specified number of decimal places. It has two parameters: the expression and the number of digits used in rounding. Test this function by typing the following statements in the Immediate window: |
|
|
|
|
|
|
|
|
Print Round(202.5)_ 'Returns 202
Print Round(202.56) 'Returns 203
Print Round(202.56,1) 'Returns 202.6
Print Round(202.56,2) 'Returns 202.56 |
|
|
|
|
|
|
|
|
As you can see, the Round function is easy to use. The only thing you need to remember is that your results may still need additional formatting if you always want to display digits after a decimal. In other words, no digits are printed after the decimal if you execute this line of code: |
|
|
|
|
|
|
|
|
Print Round(202,4)_ 'Returns 202 no decimal places |
|
|
|
|
|
|
|
|
Using the Format Function |
|
|
|
|
|
|
|
|
The Format function is more versatile than the previously discussed functions. Format is a single function that can handle and return dates, numbers, and strings. Rather than relying on a bunch of optional parameters, the Format function uses a control string, which can represent either a user-defined or named (system-defined) format. |
|
|
|
|
|
|
|
|
The most common use of the Format function is to place numbers in a particular format to make them easier for users to read. To use the Format function, you specify the input value and the format, as shown in the following lines of code: |
|
|
|
|
|
|
|
|
Printer.Print Format(GrossSales, "Currency")
Printer.Print Format(GrossSales, "$####.00") |
|
|
|
|
|
|
|
|
To work with numbers, you can use several named formats. In all cases, you must enclose the name of the format in double quotation marks. Table 9.5 shows the named formats for numbers that are available with the Format function. Figure 9.6 shows how each named format displays several different numbers. |
|
|
|
|
|